// const { think } = require('thinkjs');
const { default: BigNumber } = require('bignumber.js');
// const moment = require('moment');
const { think } = require('thinkjs');
const Base = require('./base.js');

module.exports = class extends Base {
  async companyAction() {
    const query = {
      user_id: this.getLoginUserId() || 9159,
    };
    const uData = await this.model('company_interview')
      .order(['a.id DESC'])
      .alias('a')
      .join({
        company_list: {
          join: 'left',
          as: 'c',
          on: ['company_id', 'id'],
        },
        post_list: {
          join: 'left',
          as: 'p',
          on: ['job_id', 'id'],
        },
      })
      .field(
        'a.id as interviewId, a.user_id, a.status as interview_status, a.startTime, a.type, c.*, p.type as job_type, p.money_start, p.money_end, p.name as job_name'
      )
      .where(Object.assign(query, { 'a.status': ['IN', '0,1'] }))
      .select();

    for (const item of uData) {
      if (item.fuli) {
        item.tags = await this.model('tag_list')
          .where({
            id: ['IN', item.fuli],
          })
          .field('name, icon')
          .select();
      }
      item.startTime = moment(item.startTime * 1000).format('MM-DD HH:MM');
      if (
        moment(item.startTime * 1000)
          .add(5, 'm')
          .isBefore(moment()) &&
        item.interview_status === 1
      ) {
        item.begin = true;
      }
    }

    const data = await this.model('company_interview')
      .order(['a.id DESC'])
      .alias('a')
      .join({
        company_list: {
          join: 'left',
          as: 'c',
          on: ['company_id', 'id'],
        },
        post_list: {
          join: 'left',
          as: 'p',
          on: ['job_id', 'id'],
        },
      })
      .field(
        'a.id as interviewId, a.user_id, a.status as interview_status, a.startTime, a.type, c.*, p.type as job_type, p.money_start, p.money_end, p.name as job_name'
      )
      .where(Object.assign(query, { 'a.status': ['NOTIN', '0,1'] }))
      .select();

    for (const item of data) {
      if (item.fuli) {
        item.tags = await this.model('tag_list')
          .where({
            id: ['IN', item.fuli],
          })
          .field('name')
          .select();
      }
      item.startTime = moment(item.startTime * 1000).format('MM-DD HH:MM');
    }

    const has = await this.model('process')
      .where({
        user_id: this.getLoginUserId(),
        status: 0,
      })
      .find();

    return this.success({
      uView: uData,
      hView: data,
      has: !think.isEmpty(has),
    });
  }

  async jobCompanyAction() {
    const id = this.get('id');
    const data = await this.model('company_interview')
      .order(['a.id DESC'])
      .alias('a')
      .join({
        company_list: {
          join: 'left',
          as: 'c',
          on: ['company_id', 'id'],
        },
        post_list: {
          join: 'left',
          as: 'p',
          on: ['job_id', 'id'],
        },
        industry_list: {
          join: 'left',
          as: 'i',
          on: ['c.industry', 'id'],
        },
      })
      .field(
        'i.name as industry_name, a.addr, c.scale, c.city, a.type, a.startTime, a.status, c.id as company_id, c.name, c.avatar, p.money_start, p.money_end, p.name as job_name'
      )
      .where({ 'a.id': id })
      .find();

    data.startTime = moment(data.startTime * 1000).format('YYYY-MM-DD HH:MM');

    if (
      moment(data.startTime * 1000)
        .add(5, 'm')
        .isBefore(moment()) &&
      data.status === 1
    ) {
      data.begin = true;
    }

    return this.success(data);
  }

  async companyUpdateAction() {
    const id = this.post('id');
    const type = this.post('type');
    const data = await this.model('company_interview')
      .alias('a')
      .join({
        post_list: {
          join: 'left',
          as: 'p',
          on: ['job_id', 'id'],
        },
      })
      .where({ 'a.id': id, 'a.user_id': this.getLoginUserId() })
      .field(
        'a.status, p.name as job_name, a.startTime, a.endTime, a.company_id'
      )
      .find();

    if (!think.isEmpty(data)) {
      //拒绝
      if (data.status === 0 && type === 'refuse') {
        await this.model('company_interview').where({ id: id }).update({
          status: 2,
        });
      }
      //接受
      if (data.status === 0 && type === 'accept') {
        const user = await this.model('user_list')
          .where({ id: this.getLoginUserId() })
          .find();
        if (!user.userToken) {
          const userToken = await this.service(
            'video_process',
            'admin'
          ).createUser(cid(24), 2, user.name);

          user.userToken = userToken.userToken;
          this.model('user_list').where({ id: this.getLoginUserId() }).update({
            userToken: userToken.userToken,
            platformUserId: userToken.platformUserId,
          });
        }
        //创建视频面试
        const requestId = cid(24);
        const thirdOrderNo = think.uuid();
        const company = await this.model('company_list')
          .where({ id: data.company_id })
          .find();

        const aiInterview = await this.service(
          'video_process',
          'admin'
        ).createInterview({
          requestId: requestId,
          thirdOrderNo: thirdOrderNo,
          title: data.job_name + '的面试',
          startTime: data.startTime,
          endTime: data.endTime,
          userToken: user.userToken,
          interviewerToken: company.userToken,
        });

        if (aiInterview.data.code === '0') {
          await this.model('company_interview')
            .where({ id: id })
            .update({
              status: 1,
              requestId: requestId,
              thirdOrderNo: thirdOrderNo,
              title: data.job_name + '的面试',
              interviewToken: aiInterview.data.interviewToken,
            });
        } else {
          return this.fail('操作失败');
        }
      }
    } else {
      return this.fail('操作失败');
    }
    return this.success();
  }
};
